<template>
  <div class="Navtop">
    <div class="nav_top">
      <div class="left_box">
        <ul>
          <li>
            <router-link to="">品牌</router-link>
          </li>
          <li>
            <router-link to="">OriginOS</router-link>
          </li>
          <li>
            <router-link to="">体验店</router-link>
          </li>
          <li>
            <router-link to="">社区</router-link>
          </li>
        </ul>
      </div>
      <div class="right_box">
        <ul>
          <li>
            <router-link to="">
              <i class="el-icon-download">下载APP</i>
            </router-link>
          </li>
          <li>
            <template>
              <el-popover
                placement="bottom"
                :width="popW"
                trigger="hover"
                @show="getShopFn"
              >
                <div v-show="flag" style="text-align: center">
                  <h1>请先登录</h1>
                </div>
                <el-table :data="tableData" v-show="!flag">
                  <!-- <el-table-column
                    prop="id"
                    label="商品id"
                    width="100"
                    align="center"
                  ></el-table-column> -->
                  <el-table-column
                    prop="s_good.name"
                    label="商品名称"
                    width="150"
                    align="center"
                  ></el-table-column>
                  <el-table-column
                    prop="s_good.price"
                    label="价格(元)"
                    width="150"
                    align="center"
                  ></el-table-column>
                  <el-table-column
                    prop="num"
                    label="数量"
                    width="150"
                    align="center"
                  >
                    <template slot-scope="scope">
                      <el-input-number
                        v-model="scope.row.num"
                        @change="handleChange(scope.row.num, scope.row.id)"
                        size="mini"
                        :min="1"
                      ></el-input-number>
                    </template>
                  </el-table-column>
                </el-table>

                <el-button slot="reference" class="Car" @click="skipCar"
                  ><i class="el-icon-shopping-cart-2"></i>购物车</el-button
                >
              </el-popover>
            </template>
          </li>

          <!-- 登录之前   v-if="!this.$store.state.flag" -->
          <li v-if="!this.$store.state.flag">
            <p>
              <router-link to="/login">登录</router-link>

              /

              <router-link to="/register">注册</router-link>
            </p>
          </li>
          <!-- 登录之后   v-if="this.$store.state.flag" -->
          <li v-if="this.$store.state.flag">
            <p>
              <span @click="skipOrderFn">
                <router-link to="/personal/realOrder">我的订单</router-link>
              </span>
              /
              <span>
                <router-link to="/personal/shoppingAddress"
                  >地址管理</router-link
                >
              </span>
              /
              <router-link to="/personal/info">账户中心</router-link>

              /
              <span @click="loginOut">
                <router-link to=""> 退出登陆 </router-link>
              </span>
            </p>
          </li>
        </ul>

        <!-- <div
          class="userbox"
          @mouseleave="leave"
          v-if="!this.$store.state.flag"
        >
          <div class="jiantou"></div>
          <p>
            <i class="el-icon-sort">
              <router-link to="/login">登录</router-link>
            </i>
          </p>
          <p>
            <i class="el-icon-s-check">
              <router-link to="/register">注册</router-link>
            </i>
          </p>
        </div> -->

        <!-- 登录之后   v-if="this.$store.state.flag" -->
        <!-- <div
          class="userbox"
          v-show="userFlag"
          @mouseleave="leave"
          v-if="this.$store.state.flag"
        >
          <div class="jiantou"></div>
          <p>
            <i class="el-icon-sort" @click="skipOrderFn">
              <router-link to="">我的订单</router-link>
            </i>
          </p>
          <p>
            <i class="el-icon-s-check">
              <router-link to="/personal">账户中心</router-link>
            </i>
          </p>
          <p>
            <i class="el-icon-s-check" @click="loginOut">
              <router-link to="">退出登陆</router-link>
            </i>
          </p>
        </div> -->
      </div>
    </div>
  </div>
</template>

<script>
import { loginOut } from "../api/test";
import { shopCar, shopCarUpdate } from "../api/test";

export default {
  name: "Navtop",
  data() {
    return {
      time: 3,
      tableData: [],
      flag: true,
      // falsh: false,
      popW: 200,
    };
  },
  created() {
    shopCar({
      project_id: 149,
    }).then((res) => {
      console.log(res.data.result);
      this.tableData = res.data.result;
      this.loading = false;
    });
    if (sessionStorage.getItem("x-toekn")) {
      this.falsh = true;
    } else {
      this.falsh = false;
    }
  },
  methods: {
    getShopFn() {
      shopCar({
        project_id: 149,
      })
        .then((res) => {
          this.flag = false;
          (this.popW = 500), (this.tableData = res.data.result);
          this.loading = false;
        })
        .catch((err) => {
          this.flag = true;
          (this.popW = 200), console.log(err);
        });
    },
    skipCar() {
      if (sessionStorage.getItem("user")) {
        this.$router.push({ path: "/personal/shopCar" });
      } else {
        this.$message({
          message: `请先登陆哦！即将跳转至登陆页`,
          type: "warning",
        });
        setTimeout(() => {
          this.$router.push({ path: "/login" });
        }, 1000);
      }
    },
    loginOut() {
      this.$confirm("此操作将退出登录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        let res = await loginOut({});
        this.$router.push({
          path: "/login",
        });
        this.$store.commit("outLogin");
        sessionStorage.removeItem("user");
        this.$message({
          type: "success",
          message: res.data.msg,
        });
      });
    },

    skipOrderFn() {
      this.$router.push({ path: "/personal/realOrder" });
    },
    //数量变化
    handleChange(shopNum, shopId) {
      // console.log(shopNum,shopId);
      shopCarUpdate(shopId, {
        num: shopNum,
      }).then((res) => {
        // console.log(res);
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.nav_top {
  padding: 0 5%;
  background-color: #242933;
  height: 40px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .left_box {
    ul {
      display: flex;
      li {
        margin-right: 50px;
        font-size: 12px;

        a {
          color: #838892;
          text-decoration: none;
        }
      }
    }
  }
  .right_box {
    ul {
      display: flex;
      align-items: center;
      li {
        margin-left: 50px;
        font-size: 12px;
        a {
          color: #838892;
          text-decoration: none;
        }

        button {
          width: 60px;
          position: relative;
          background-color: transparent;
          border: none;
          height: 17px;
          font-size: 12px;
          color: #838892;
          span {
            top: 5px;
            left: 5px;
            position: absolute;
          }
        }
      }
    }
  }
}

.right_box {
  position: relative;
  .userbox {
    position: absolute;
    z-index: 1000;
    top: 40px;
    right: -70px;
    width: 200px;
    height: 80px;
    border-radius: 20px;
    background-color: #e9eefa;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
    .jiantou {
      position: absolute;
      border: 10px solid;
      border-color: transparent transparent #e9eefa transparent;
      z-index: 1000;
      top: -20px;
      right: 50%;
      transform: translateX(50%);
    }
    p {
      text-align: center;
      font-size: 16px;
      width: 200px;
      a {
        margin: 0 10px;
        transition: all 0.5s;
        color: #000;
      }
      :hover {
        color: aqua;
        transition: all 0.5s;
      }
    }
  }
  .Car {
    margin-top: -10px;
  }
}
</style>
